package com.yingjia.yingjiauniversity.json.manager;

import com.yingjia.yingjiauniversity.json.JsonComponent;
import com.yingjia.yingjiauniversity.json.gson.GsonComponentImpl;
import com.yingjia.yingjiauniversity.json.jackson.JacksonComponentImpl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by 张智轶 on 2016/10/11.
 */
public class JsonManager {
    private static volatile JsonManager jsonManager;
    private JsonComponent component;
    private Logger logger = LoggerFactory.getLogger(getClass());

    public static JsonManager getInstance() {
        if (jsonManager == null) {
            synchronized (JsonManager.class) {
                if (jsonManager == null) {
                    jsonManager = new JsonManager();
                }
            }
        }
        return jsonManager;
    }

    public void setDefaultComponent(String name) {
        synchronized (this) {
            if (name.toLowerCase().equals("jackson") && !(component instanceof JacksonComponentImpl))
                component = JacksonComponentImpl.getInstance();
            else if (name.toLowerCase().equals("gson") && !(component instanceof GsonComponentImpl))
                component = GsonComponentImpl.getInstance();
            else
                logger.debug("JsonManager setDefault: name not supported");
        }
    }

    public JsonComponent getComponent() {
        synchronized (this) {
            if (component == null) {
                component = JacksonComponentImpl.getInstance();
            }
            return component;
        }
    }
}
